let app = getApp();
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    usClass:true, //修改的样式
    goodsId:null,
    storeName:'',
    longitude:'', //经纬度
    latitude: '',
    timer: '',
    storeListByGoods:[],
    reply:[],
    isLoadMore: true,
    page:1,
    totalPage:0,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.goodsId??'') === '' && (options.longitude??'') === '' &&(options.latitude??'') === '') {
      return 
    }
    this.setData({
      goodsId: parseInt(options.goodsId),
      storeName: '',
      longitude: options.longitude,
      latitude: options.latitude,
    })
    this.getStoreListByGoods();
  },
  getStoreListByGoods() {
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store.getStoreListByGoods");
    let json = {
      goods_id:this.data.goodsId,
      store_name: this.data.storeName,
      longitude: this.data.longitude,
      latitude: this.data.latitude
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            storeListByGoods: res.data.data,
            total:res.data.total

          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  searchChange({ detail }) {
    this.setData({ 
      storeName: detail,
      page:1,
      isLoadMore:true,
    });
    // 防抖
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.getStoreListByGoods());
    }, 500);
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store.getStoreListByGoods");
      let json = {
        goods_id:this.data.goodsId,
        store_name: this.data.storeName,
        longitude: this.data.longitude,
        latitude: this.data.latitude,
        page:this.data.page
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              storeListByGoods : this.data.storeListByGoods.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1,
      storeName: ""
    })
    this.getStoreListByGoods();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})